const artModel = require('../../model/art/artModel.js')
class artService{
  //搜索所有艺术涂料
  async getArt(req,res){
    const {results:data1} = await artModel.art()
    const {results:data2} = await artModel.details()
    
    function Binary(arr1,arr2,name){
      for(let i=0;i<arr1.length;i++){
        arr1[i][name] = []
        for(let j=0;j<arr2.length;j++){
          if(arr1[i].id == arr2[j].father){
            arr1[i][name].push(arr2[j])
          }
        }
      }
      return arr1
    }
    const arrData = Binary(data1,data2,'details')
    const art ={
      arrData,
      count:arrData.length,
    }
    if(data1.length !=0){
      return res.json({
        art,
        meta:{
          msg:'用户信息获取成功',
          status:200
        }
      })
    }else{
      return res.json({
        meta:{
          msg:'用户信息获取失败',
          status:404
        }
      })
    }
  }
  //查询艺术涂料 query pagenum第几页 pagesize每页几人
  async postArt(req,res){
    let query = req.body.query
    let pagenum = parseInt(req.body.pagenum)
    let pagesize = parseInt(req.body.pagesize)
    if(pagesize === 1){
      pagenum = pagenum-1
    }else if(pagesize === 2){
      pagenum = (pagenum-1)*2
    }else if(pagesize === 5){
      pagenum = (pagenum-1)*5
    }else if(pagesize === 10){
      pagenum = (pagenum-1)*10
    }
    const {results:data} = await artModel.queryArt(query,pagenum,pagesize)
    const {results:data1} = await artModel.queryArtTotal(query)
    const {results:data2} = await artModel.details()
    // const {results:data3} = await artModel.item()
    
    function Binary(arr1,arr2,name){
      for(let i=0;i<arr1.length;i++){
        arr1[i][name] = []
        for(let j=0;j<arr2.length;j++){
          if(arr1[i].id == arr2[j].father){
            arr1[i][name].push(arr2[j])
          }
        }
      }
      return arr1
    }
    // Binary(data2,data3,'item')
    const arrData = Binary(data,data2,'details')

    let datas = {
      'pagenum':pagenum,
      'total':data1.length,
      'art':arrData
    }
    if(datas.length != 0){
      return res.json({
        datas,
        meta: {
          msg: '获取用户信息获取成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '获取用户信息获取失败',
          status: 404
        }
      })
    }
  }

  //添加艺术涂料
  async postAddArt(req,res){
      //Art
      console.log(req.body)
    const {title,img,EnglishName,classify} = req.body
    const {results:data} = await artModel.addArt(title,img,EnglishName,classify)
    console.log(data.insertId)
    //details
    const father = data.insertId
    const title2 = req.body.details.title
    const img2 = req.body.details.img
    const article = req.body.details.article
    const {results:data2} = await artModel.addDetails(father,title2,img2,article)
    

    // console.log(req.body.list)
    if(data.length != 0){
      return res.json({
        data,
        data2,
        meta: {
          msg: '轮播图新增成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '轮播图新增失败',
          status: 404
        }
      })
    }
  }

  //id查询艺术涂料
  async getArtID(req,res){
    const id = req.query.id
    console.log(req.query)
    const {results:data1} = await artModel.artID(id)
    console.log( data1[0].id)
    const {results:data2} = await artModel.detailsID(data1[0].id)

    function Binary(arr1,arr2,name){
      for(let i=0;i<arr1.length;i++){
        arr1[i][name] = []
        for(let j=0;j<arr2.length;j++){
          if(arr1[i].id == arr2[j].father){
            arr1[i][name].push(arr2[j])
          }
        }
      }
      return arr1
    }

    const arrData = Binary(data1,data2,'details')
    if(data1.length !=0){
      return res.json({
        arrData,
        meta:{
          msg:'用户信息获取成功',
          status:200
        }
      })
    }else{
      return res.json({
        meta:{
          msg:'用户信息获取失败',
          status:404
        }
      })
    }
  }

  //修改艺术涂料
  async postUpdateArt(req,res){
    console.log(req.body)
    const art = req.body
    const {results:data} = await artModel.updateArt(art.title,art.img,art.EnglishName,art.classify,art.id)
    const details = req.body.details[0]
    const {results:data2} = await artModel.updateDetails(details.title,details.img,details.article,details.father)
    if(data.length != 0){
      res.json({
        data:data[0],
        meta:{
          msg: '艺术涂料修改成功',
          status: 200
        }
      })
    }else{
      res.json({
        meta:{
          msg: '艺术涂料修改失败',
          status: 404
        }
      })
    }
    

  }

  //删除艺术涂料
  async postDeleteArt(req,res){
    const id = req.body.id
    const {results:data} = await artModel.deleteArt(id)

    const {results:data2} = await artModel.detailsID(id)

    const {results:data4} = await artModel.deleteDetails(id)

    if(data.length !=0){
      return res.json({
        meta: {
          msg: '删除艺术涂料成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '删除艺术涂料失败',
          status: 404
        }
      })
    }
  }
  
}
module.exports = new artService()